package com.raymond.pinyougou.sellergoods.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.raymond.pinyougou.entity.PageResult;
import com.raymond.pinyougou.mapper.TbBrandMapper;
import com.raymond.pinyougou.pojo.TbBrand;
import com.raymond.pinyougou.pojo.TbBrandExample;
import com.raymond.pinyougou.sellergoods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @Author Raymond
 * @Description //TODO
 * @Date 2019/3/22 23:31
 * @Version 1.0
 **/
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper brandMapper;

    @Override
    public List<TbBrand> findAll() {
        return brandMapper.selectByExample(null);
    }

    @Override
    public PageResult findPage(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public boolean addBrand(TbBrand brand) {
        int count = brandMapper.insert(brand);
        if(count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public TbBrand getBrand(long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateBrand(TbBrand brand) {
        if(brandMapper.updateByPrimaryKey(brand) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteBrand(long[] ids) {
        if (brandMapper.delBrand(ids) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public PageResult findPage(TbBrand brand, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        TbBrandExample example=new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if(brand != null) {
            if(!StringUtils.isEmpty(brand.getName())) {
                criteria.andNameLike("%" + brand.getName() + "%");
            }
            if(!StringUtils.isEmpty(brand.getFirstChar())) {
                criteria.andFirstCharLike("%" + brand.getFirstChar() + "%");
            }
        }
        Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Map> selectOptionList() {
        return brandMapper.selectOptionList();
    }
}
